cmake_minimum_required(VERSION 2.8)

PROJECT(backtory C)

file(READ ${backtory_SOURCE_DIR}/src/include/backtory/version.h BACKTORY_VERSION_H_CONTENTS)
string(REGEX MATCH "BACKTORY_VERSION_FLAVOUR[ \t]+\"([A-Z]+)\"" BACKTORY_VERSION_FL ${BACKTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "(CURRENT|STABLE|ALPHA|BETA|PRERELEASE|RELEASE)" BACKTORY_VERSION_FL ${BACKTORY_VERSION_FL})
string(REGEX MATCH "BACKTORY_VERSION_MAJOR[ \t]+([0-9]+)" BACKTORY_VERSION_MJ ${BACKTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" BACKTORY_VERSION_MJ ${BACKTORY_VERSION_MJ})
string(REGEX MATCH "BACKTORY_VERSION_MINOR[ \t]+([0-9]+)" BACKTORY_VERSION_MI ${BACKTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" BACKTORY_VERSION_MI ${BACKTORY_VERSION_MI})
string(REGEX MATCH "BACKTORY_VERSION_PATCH[ \t]+([0-9]+)" BACKTORY_VERSION_PT ${BACKTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" BACKTORY_VERSION_PT ${BACKTORY_VERSION_PT})
string(REGEX MATCH "BACKTORY_VERSION_SUFFX[ \t]+([0-9]+)" BACKTORY_VERSION_SX ${BACKTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" BACKTORY_VERSION_SX ${BACKTORY_VERSION_SX})

string(COMPARE EQUAL ${BACKTORY_VERSION_FL} "RELEASE" BACKTORY_RELEASE)
if(BACKTORY_RELEASE)
	set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}.${BACKTORY_VERSION_PT})
else(BACKTORY_RELEASE)
	string(COMPARE EQUAL ${BACKTORY_VERSION_FL} "PRERELEASE" BACKTORY_PRERELEASE)
	if(BACKTORY_PRERELEASE)
		set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}.${BACKTORY_VERSION_PT}-rc${BACKTORY_VERSION_SX})
	else(BACKTORY_PRERELEASE)
		string(COMPARE EQUAL ${BACKTORY_VERSION_FL} "BETA" BACKTORY_BETA)
		if(BACKTORY_BETA)
			set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}.${BACKTORY_VERSION_PT}-b${BACKTORY_VERSION_SX})
		else(BACKTORY_BETA)
			string(COMPARE EQUAL ${BACKTORY_VERSION_FL} "ALPHA" BACKTORY_ALPHA)
			if(BACKTORY_ALPHA)
				set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}.${BACKTORY_VERSION_PT}-a${BACKTORY_VERSION_SX})
			else(BACKTORY_ALPHA)
				string(COMPARE EQUAL ${BACKTORY_VERSION_FL} "STABLE" BACKTORY_STABLE)
				if(BACKTORY_STABLE)
					set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}-STABLE)
				else(BACKTORY_STABLE)
					set(BACKTORY_VERSION ${BACKTORY_VERSION_MJ}.0-CURRENT)
				endif(BACKTORY_STABLE)
			endif(BACKTORY_ALPHA)
		endif(BACKTORY_BETA)
	endif(BACKTORY_PRERELEASE)
endif(BACKTORY_RELEASE)

# find_program(HG hg)
# 
# if(HG)
# 	execute_process(
# 		COMMAND ${HG} identify -i
# 		OUTPUT_VARIABLE BACKTORY_REVISION
# 		OUTPUT_STRIP_TRAILING_WHITESPACE
# 	)
# 	execute_process(
# 		COMMAND ${HG} identify -b
# 		OUTPUT_VARIABLE BACKTORY_BRANCH
# 		OUTPUT_STRIP_TRAILING_WHITESPACE
# 	)
# else(HG)
# 	set(BACKTORY_REVISION "-1")
# 	set(BACKTORY_BRANCH "unknown")
# endif(HG)
# 
# string(TOUPPER ${BACKTORY_BRANCH} BACKTORY_BRANCH)
# string(REGEX MATCH "DEFAULT" BACKTORY_BRANCH_MATCH ${BACKTORY_BRANCH})
# if(BACKTORY_BRANCH_MATCH)
# 	set(BACKTORY_VERSION "${BACKTORY_VERSION_MJ}.0-${BACKTORY_VERSION_FL} (${BACKTORY_REVISION})")
# endif(BACKTORY_BRANCH_MATCH)
# string(REGEX MATCH "^RELENG_[0-9]+$" BACKTORY_BRANCH_MATCH ${BACKTORY_BRANCH})
# if(BACKTORY_BRANCH_MATCH)
# 	set(BACKTORY_VERSION "${BACKTORY_VERSION_MJ}.${BACKTORY_VERSION_MI}-STABLE (${BACKTORY_REVISION})")
# endif(BACKTORY_BRANCH_MATCH)


set(PACKAGE "backtory")
set(PACKAGE_NAME ${PACKAGE})
set(PACKAGE_VERSION ${BACKTORY_VERSION})

set(CMAKE_C_FLAGS "${CMAKE_ANSI_CFLAGS} ${CMAKE_C_FLAGS}")
set(CMAKE_REQUIRED_FLAGS ${CMAKE_ANSI_CFLAGS})
set(CMAKE_REQUIRED_LIBRARIES ${BACKTORY_LIBS})
set(BACKTORY_LIBS "")

add_subdirectory(doc)
add_subdirectory(src)